package com.lpjeremy.uimodule.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;

/**
 * @desc:扫码使用的EditText
 * @date:2019/4/30 10:40
 * @auther:lp
 * @version:1.1.6
 */
public class ScanEditText extends AppCompatEditText {
    private String beforeText = "";
    private String afterText = "";
    private OnEditTextChangedAndActionListener onEditTextChangedAndActionListener;
    /**
     * 是否已通知
     */
    private boolean informed = false;

    public ScanEditText(Context context) {
        super(context);
    }

    public ScanEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScanEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setImeOptions(EditorInfo.IME_ACTION_SEND);

        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                beforeText = charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                afterText = editable.toString();
                Log.d("NBA", "beforeText = " + beforeText);
                Log.d("NBA", "afterText = " + afterText);
                if (TextUtils.isEmpty(beforeText) && !TextUtils.isEmpty(afterText) && afterText.length() > 6) {
                    if (onEditTextChangedAndActionListener != null) {
                        Log.d("NBA", "textChanged这里执行了");
                        informed = true;
                        onEditTextChangedAndActionListener.onTextChanged(afterText);
                    }
                }
            }
        });

        setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {

                    if (informed) {
                        informed = false;
                    } else {
                        if (onEditTextChangedAndActionListener != null) {
                            Log.d("NBA", "action 这里执行了");
                            onEditTextChangedAndActionListener.onEnterAction(getText().toString().trim());
                        }
                    }
                }
                return false;
            }
        });

    }

    public void setOnEditTextChangedAndActionListener(OnEditTextChangedAndActionListener onEditTextChangedAndActionListener) {
        this.onEditTextChangedAndActionListener = onEditTextChangedAndActionListener;
    }

    /**
     * 文本变化和键盘按钮操作监听
     */
    public interface OnEditTextChangedAndActionListener {
        /**
         * 文本发生变化后，从无到有并且长度大于5
         */
        void onTextChanged(String value);

        /**
         * 回车按钮监听
         */
        void onEnterAction(String value);
    }
}
